import os

import xlrd
import xlwt


def readExcel(f):
  bok = xlrd.open_workbook(f)
  return bok



def createdExcelFile(name, order_arr):
  wb = xlwt.Workbook()  # 创建excel文件
  sheet = wb.add_sheet(name)  # 为第一个表命名

  sheet.row(0).height_mismatch = True
  sheet.row(0).height = 30*20
  sheet.col(0).width = 256*72
  style = xlwt.XFStyle()  # 创建一个样式对象，初始化样式
  al = xlwt.Alignment()
  al.horz = 0x02      # 设置水平居中
  al.vert = 0x01      # 设置垂直居中
  style.alignment = al
  sheet.write(0, 0, '项目',style)
  sheet.write(0, 1, '项目咨询',style)
  for i in range(len(order_arr)):
    sheet.row(i+1).height_mismatch = True
    sheet.row(i+1).height = 30*20
    sheet.col(i+1).width = 256*72
    # if '项目驻场：' in order_arr[i]['phone']:
    #   newPhone = order_arr[i]['phone']
    #   newPhone = newPhone[5:len(newPhone)]
    #   sheet.write(i + 1, 0, order_arr[i]['name'])
    #   sheet.write(i + 1, 1, newPhone)
    # else:
    sheet.write(i + 1, 0, order_arr[i]['name'],style)
    sheet.write(i + 1, 1, order_arr[i]['phone'],style)
    # print('i_' + str(i), order_arr[i])


  file_path = os.path.join(os.getcwd(), 'G:\\work\\vuework\\crash-handler-management\\pythonManger\\')  # 指定要保存的目录

  if not os.path.exists(file_path):  # 如果目录不存在，生成
    os.mkdir(file_path)
  file_path2 = file_path + 'new楼盘全部.xls'  # 文件的绝对路径
  wb.save(file_path2)


def selectStrOne(order_nums,order_phone_nums):
  loupanArr = []
  for i in range(len(order_nums)):
    if i > 0:
      item = order_nums[i]
      item_phone = order_phone_nums[i]
      if item != '':
        newPhone = item_phone
        if '项目驻场：' in newPhone:
          lle = newPhone.index('项目驻场：',0,len(newPhone))
          if lle == 0:
            newPhone = newPhone[5:len(newPhone)]
          else:
            newPhone = newPhone[0:len(newPhone)]
        # if '项目咨询：' in newPhone:
        #   llle = newPhone.index('项目咨询：',0,len(newPhone))
        #   if llle == 0:
        #     newPhone = newPhone[5:len(newPhone)]
        #   else:
        #     newPhone = newPhone[0:len(newPhone)]
        if '项目经理：' in newPhone:
          le = newPhone.index('项目经理：',0,len(newPhone))
          if le != 0:
            newPhone = newPhone[0:le]
      loupan = {
        'name':item,
        'phone': newPhone
      }
      loupanArr.append(loupan)
    # print('loupanArr', loupanArr)
  createdExcelFile('全部', loupanArr)


if __name__ == '__main__':
  path = r'G:\work\vuework\crash-handler-management\pythonManger\楼盘全部.xls'
  sheets = readExcel(path)
  sheet = sheets.sheet_by_index(0)
  rows_data = sheet.row_values(0)
  colx = 0
  phone_colx = 0
  for i in range(len(rows_data)):
    item = rows_data[i]
    # print('item', item)
    if item == '项目':
      colx = i
    if '咨询' in item:
      phone_colx = i
  order_nums = sheet.col_values(colx)
  order_phone_nums = sheet.col_values(phone_colx)
  selectStrOne(order_nums,order_phone_nums)
